﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using InIReaderExample.Properties;

namespace InIReaderExample
{
	public partial class FrmRename : Form
	{
		private readonly IEnumerable<string> names;

		/// <summary>
		/// Initializes a new instance of the <see cref="FrmRename" /> class.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="names">The names.</param>
		public FrmRename(string name, IEnumerable<string> names)
		{
			InitializeComponent();

			this.names = names;
			tbName.Text = name;
			lblInfo.Text = Resources.FrmRename_BtnOkClick_The_name_does_alreade_exists__please_choose_another_;
		}

		/// <summary>
		/// BTNs the ok click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
		private void BtnOkClick(object sender, EventArgs e)
		{
			if (String.IsNullOrWhiteSpace(tbName.Text))
			{
				lblInfo.Text = Resources.frmRename_BtnOkClick_The_new_name_must_not_be_empty_;
				tbName.Focus();
			}
			else if (names.Contains(tbName.Text))
			{
				lblInfo.Text = Resources.FrmRename_BtnOkClick_The_name_does_alreade_exists__please_choose_another_;
				tbName.Focus();
			}
			else
			{
				DialogResult = DialogResult.OK;
				Close();
			}
		}

		/// <summary>
		/// BTNs the cancel click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
		private void BtnCancelClick(object sender, EventArgs e)
		{
			Close();
		}

		private void TbNameTextChanged(object sender, EventArgs e)
		{
			if (String.IsNullOrWhiteSpace(tbName.Text))
			{
				lblInfo.Text = Resources.frmRename_BtnOkClick_The_new_name_must_not_be_empty_;
			}
			else if (names.Contains(tbName.Text))
			{
				lblInfo.Text = Resources.FrmRename_BtnOkClick_The_name_does_alreade_exists__please_choose_another_;
			}
			else
			{
				lblInfo.Text = Resources.FrmRename_TbNameTextChanged_Please_enter_the_new_Name_below_;
			}
		}

		/// <summary>
		/// FRMs the rename key press.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="KeyPressEventArgs" /> instance containing the event data.</param>
		private void FrmRenameKeyPress(object sender, KeyPressEventArgs e)
		{
			switch (e.KeyChar)
			{
				case (char)Keys.Enter:
					BtnOkClick(sender, null);
					break;

				case (char)Keys.Escape:
					BtnCancelClick(sender, null);
					break;
			}

		}

		public string FileName { get { return tbName.Text; } }

	}
}